const fs = require('fs');
const path = require('path');
const unirest = require('unirest');



//需要抓取的城市数据
const need_fetch_cities = [ { name:'chengdu',url: 'http://www.dianping.com/shoplist/search/8_10_0_score '},{
  name: 'beijing',url: 'http://www.dianping.com/shoplist/search/2_10_0_score'
},  { name: 'guangzhou' ,url: 'http://www.dianping.com/shoplist/search/4_10_0_score'},
  { name: 'shenzhen', url: 'http://www.dianping.com/shoplist/search/7_10_0_score'},]


var cities = [],shops =[];

for (const city of need_fetch_cities) {
  let p = path.join(process.cwd(), 'cities1', `${city.name}.json`);
  if(fs.existsSync(p)){
    let data = require(p);
    if(data.shopBeans && data.shopBeans.length > 0){
      let obj = Object.create(null);
      obj.city_name = city.name;
      obj.shops = [];
      for (let shop of data.shopBeans) {
        obj.shops.push({
          city_name :city.name,
          shopId:shop.shopId
        }) ;
        shops.push(Object.assign({cityName :city.name},shop));
      }
      cities.push(obj);
    }
  }
}


var p = path.join(process.cwd(),"backup","shop_comments");


var files = fs.readdirSync(p);


var len = 0;

if (files && files.length  > 0){
  for (let file of files){
    let data = require(path.join(p,file));
    let sid = path.basename(file,'.json');
    let m = shops.find(q => q.shopId.toString() === sid);
    if (m){
      let shopData = Object.assign({
        dishTagStrList: data.dishTagStrList,
        reviewCountPic: data.reviewCountPic,
        reviewCountAll: data.reviewCountAll,
        reviewCountCommon: data.reviewCountCommon,
        reviewCountBad: data.reviewCountBad,
        reviewAllDOList: data.reviewAllDOList,
        summarys:data.summarys,
        reviewCountForMore: data.reviewCountForMore,
      }, m);
      unirest.post(`http://127.0.0.1:7001/shop`)
      .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
      .send(shopData)
      .end(function (response) {
        console.log(response);
        len = len + 1;
        console.log(` len:${len}`);
      });
    }
  }

}


